/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package posartenobsa;

import SQL.Insertar;
import com.sun.data.provider.DataProvider;
import com.sun.data.provider.impl.CachedRowSetDataProvider;
import com.sun.rave.faces.data.DefaultSelectItemsArray;
import com.sun.rave.web.ui.appbase.AbstractPageBean;
import com.sun.sql.rowset.CachedRowSetXImpl;
import com.sun.webui.jsf.component.Alert;
import com.sun.webui.jsf.component.Button;
import com.sun.webui.jsf.component.DropDown;
import com.sun.webui.jsf.component.Page;
import com.sun.webui.jsf.component.PageAlert;
import com.sun.webui.jsf.component.PasswordField;
import com.sun.webui.jsf.component.Tab;
import com.sun.webui.jsf.component.TextField;
import com.sun.webui.jsf.model.DefaultTableDataProvider;
import com.sun.webui.jsf.model.SingleSelectOptionsList;
import javax.faces.FacesException;
import javax.faces.event.ValueChangeEvent;
import javax.faces.validator.LongRangeValidator;

/**
 * <p>Page bean that corresponds to a similarly named JSP page.  This
 * class contains component definitions (and initialization code) for
 * all components that you have defined on this page, as well as
 * lifecycle methods and event handlers where you may add behavior
 * to respond to incoming events.</p>
 *
 * @version A_Usuario.java
 * @version Created on 16/03/2012, 11:49:41 AM
 * @author Fabio
 */
public class A_Usuario extends AbstractPageBean {


    String cc = "jdbc:postgresql://localhost:5432/WEBARTENOBSA?user=postgres&password=pokemon";
    String Nombreu, Apellidou, Username, Contraseña, Tipo, Usernamemod;
    String SQL;///
    String NNombreu, NApellidou, NContraseña, NTipo, MContraseña, DContraseña;
    Insertar in = new Insertar();

    // <editor-fold defaultstate="collapsed" desc="Managed Component Definition">
    /**
     * <p>Automatically managed component initialization.  <strong> WARNING:</strong>
     * This method is automatically generated, so any user-specified code inserted
     * here is subject to being replaced.</p>
     */
    private void _init() throws Exception {
        CBTipoUsuarioDefaultOptions.setOptions(new com.sun.webui.jsf.model.Option[]{new com.sun.webui.jsf.model.Option("ADMINISTRADOR", "ADMINISTRADOR"), new com.sun.webui.jsf.model.Option("VENDEDOR", "VENDEDOR")});
        CBTipoUsuario1DefaultOptions.setOptions(new com.sun.webui.jsf.model.Option[]{new com.sun.webui.jsf.model.Option("ADMINISTRADOR", "ADMINISTRADOR"), new com.sun.webui.jsf.model.Option("VENDEDOR", "VENDEDOR")});
        usuarioComboUSERNAMEDataProvider.setCachedRowSet((javax.sql.rowset.CachedRowSet) getValue("#{A_Usuario.usuarioComboUSERNAME}"));
        usuarioComboUSERNAME.setDataSourceName("java:comp/env/jdbc/public_PostgreSQL");
        usuarioComboUSERNAME.setCommand("SELECT username FROM usuario where usuario_activo = TRUE ");
        usuarioComboUSERNAME.setTableName("usuario");

    }
    private Page page1 = new Page();

    public Page getPage1() {
        return page1;
    }

    public void setPage1(Page p) {
        this.page1 = p;
    }
    private Button b_Guardarusuario = new Button();

    public Button getB_Guardarusuario() {
        return b_Guardarusuario;
    }

    public void setB_Guardarusuario(Button b) {
        this.b_Guardarusuario = b;
    }
    private TextField TFApellidousuario = new TextField();

    public TextField getTFApellidousuario() {
        return TFApellidousuario;
    }

    public void setTFApellidousuario(TextField tf) {
        this.TFApellidousuario = tf;
    }
    private TextField TFUsername = new TextField();

    public TextField getTFUsername() {
        return TFUsername;
    }

    public void setTFUsername(TextField tf) {
        this.TFUsername = tf;
    }
    private PasswordField TFPass1 = new PasswordField();

    public PasswordField getTFPass1() {
        return TFPass1;
    }

    public void setTFPass1(PasswordField pf) {
        this.TFPass1 = pf;
    }
    private PasswordField TFPass2 = new PasswordField();

    public PasswordField getTFPass2() {
        return TFPass2;
    }

    public void setTFPass2(PasswordField pf) {
        this.TFPass2 = pf;
    }
    private Alert alert1 = new Alert();

    public Alert getAlert1() {
        return alert1;
    }

    public void setAlert1(Alert a) {
        this.alert1 = a;
    }
    private SingleSelectOptionsList CBTipoUsuarioDefaultOptions = new SingleSelectOptionsList();

    public SingleSelectOptionsList getCBTipoUsuarioDefaultOptions() {
        return CBTipoUsuarioDefaultOptions;
    }

    public void setCBTipoUsuarioDefaultOptions(SingleSelectOptionsList ssol) {
        this.CBTipoUsuarioDefaultOptions = ssol;
    }
    private SingleSelectOptionsList CBTipoUsuario1DefaultOptions = new SingleSelectOptionsList();

    public SingleSelectOptionsList getCBTipoUsuario1DefaultOptions() {
        return CBTipoUsuario1DefaultOptions;
    }

    public void setCBTipoUsuario1DefaultOptions(SingleSelectOptionsList ssol) {
        this.CBTipoUsuario1DefaultOptions = ssol;
    }
    private Tab tabMUsuario = new Tab();

    public Tab getTabMUsuario() {
        return tabMUsuario;
    }

    public void setTabMUsuario(Tab t) {
        this.tabMUsuario = t;
    }
    private DropDown CBTipoUsuario = new DropDown();

    public DropDown getCBTipoUsuario() {
        return CBTipoUsuario;
    }

    public void setCBTipoUsuario(DropDown dd) {
        this.CBTipoUsuario = dd;
    }
    private Alert alertaContraseñasGU = new Alert();

    public Alert getAlertaContraseñasGU() {
        return alertaContraseñasGU;
    }

    public void setAlertaContraseñasGU(Alert a) {
        this.alertaContraseñasGU = a;
    }
    private Alert alertausuarioGuardado = new Alert();

    public Alert getAlertausuarioGuardado() {
        return alertausuarioGuardado;
    }

    public void setAlertausuarioGuardado(Alert a) {
        this.alertausuarioGuardado = a;
    }
    private CachedRowSetDataProvider usuarioDataProvider = new CachedRowSetDataProvider();

    public CachedRowSetDataProvider getUsuarioDataProvider() {
        return usuarioDataProvider;
    }

    public void setUsuarioDataProvider(CachedRowSetDataProvider crsdp) {
        this.usuarioDataProvider = crsdp;
    }
    private CachedRowSetDataProvider usuarioDataProvider4 = new CachedRowSetDataProvider();

    public CachedRowSetDataProvider getUsuarioDataProvider4() {
        return usuarioDataProvider4;
    }

    public void setUsuarioDataProvider4(CachedRowSetDataProvider crsdp4) {
        this.usuarioDataProvider4 = crsdp4;
    }
    private TextField TFNombreusuario_m = new TextField();

    public TextField getTFNombreusuario_m() {
        return TFNombreusuario_m;
    }

    public void setTFNombreusuario_m(TextField tf) {
        this.TFNombreusuario_m = tf;
    }
    private TextField TFApellidousuario_m = new TextField();

    public TextField getTFApellidousuario_m() {
        return TFApellidousuario_m;
    }

    public void setTFApellidousuario_m(TextField tf) {
        this.TFApellidousuario_m = tf;
    }
    private PasswordField TFPass3 = new PasswordField();

    public PasswordField getTFPass3() {
        return TFPass3;
    }

    public void setTFPass3(PasswordField pf) {
        this.TFPass3 = pf;
    }
    private CachedRowSetDataProvider usuarioComboUSERNAMEDataProvider = new CachedRowSetDataProvider();

    public CachedRowSetDataProvider getUsuarioComboUSERNAMEDataProvider() {
        return usuarioComboUSERNAMEDataProvider;
    }

    public void setUsuarioComboUSERNAMEDataProvider(CachedRowSetDataProvider crsdp) {
        this.usuarioComboUSERNAMEDataProvider = crsdp;
    }
    private CachedRowSetXImpl usuarioComboUSERNAME = new CachedRowSetXImpl();

    public CachedRowSetXImpl getUsuarioComboUSERNAME() {
        return usuarioComboUSERNAME;
    }

    public void setUsuarioComboUSERNAME(CachedRowSetXImpl crsxi) {
        this.usuarioComboUSERNAME = crsxi;
    }
    private DropDown CBSeleccion_usuario1 = new DropDown();

    public DropDown getCBSeleccion_usuario1() {
        return CBSeleccion_usuario1;
    }

    public void setCBSeleccion_usuario1(DropDown dd) {
        this.CBSeleccion_usuario1 = dd;
    }
    private PasswordField t_Pass3 = new PasswordField();

    public PasswordField getT_Pass3() {
        return t_Pass3;
    }

    public void setT_Pass3(PasswordField pf) {
        this.t_Pass3 = pf;
    }
    private DropDown CBTipoUsuario1 = new DropDown();

    public DropDown getCBTipoUsuario1() {
        return CBTipoUsuario1;
    }

    public void setCBTipoUsuario1(DropDown dd) {
        this.CBTipoUsuario1 = dd;
    }
    private PasswordField TFNPASS = new PasswordField();

    public PasswordField getTFNPASS() {
        return TFNPASS;
    }

    public void setTFNPASS(PasswordField pf) {
        this.TFNPASS = pf;
    }
    private PasswordField TFNPASS2 = new PasswordField();

    public PasswordField getTFNPASS2() {
        return TFNPASS2;
    }

    public void setTFNPASS2(PasswordField pf) {
        this.TFNPASS2 = pf;
    }
    private Alert alertaUsuarioDesactivado = new Alert();

    public Alert getAlertaUsuarioDesactivado() {
        return alertaUsuarioDesactivado;
    }

    public void setAlertaUsuarioDesactivado(Alert a) {
        this.alertaUsuarioDesactivado = a;
    }
    private Alert alertaContraseñaDes = new Alert();

    public Alert getAlertaContraseñaDes() {
        return alertaContraseñaDes;
    }

    public void setAlertaContraseñaDes(Alert a) {
        this.alertaContraseñaDes = a;
    }
    private Alert alertaUsuarioModificado = new Alert();

    public Alert getAlertaUsuarioModificado() {
        return alertaUsuarioModificado;
    }

    public void setAlertaUsuarioModificado(Alert a) {
        this.alertaUsuarioModificado = a;
    }
    private Alert alertaContraseñaMU = new Alert();

    public Alert getAlertaContraseñaMU() {
        return alertaContraseñaMU;
    }

    public void setAlertaContraseñaMU(Alert a) {
        this.alertaContraseñaMU = a;
    }
    private Alert alertaContraseñasMU = new Alert();

    public Alert getAlertaContraseñasMU() {
        return alertaContraseñasMU;
    }

    public void setAlertaContraseñasMU(Alert a) {
        this.alertaContraseñasMU = a;
    }
    private DropDown CBSeleccion_usuario = new DropDown();

    public DropDown getCBSeleccion_usuario() {
        return CBSeleccion_usuario;
    }

    public void setCBSeleccion_usuario(DropDown dd) {
        this.CBSeleccion_usuario = dd;
    }
    private Button b_Modificarusuario = new Button();

    public Button getB_Modificarusuario() {
        return b_Modificarusuario;
    }

    public void setB_Modificarusuario(Button b) {
        this.b_Modificarusuario = b;
    }
    private LongRangeValidator longRangeValidator1 = new LongRangeValidator();

    public LongRangeValidator getLongRangeValidator1() {
        return longRangeValidator1;
    }

    public void setLongRangeValidator1(LongRangeValidator lrv) {
        this.longRangeValidator1 = lrv;
    }
    private LongRangeValidator longRangeValidator2 = new LongRangeValidator();

    public LongRangeValidator getLongRangeValidator2() {
        return longRangeValidator2;
    }

    public void setLongRangeValidator2(LongRangeValidator lrv) {
        this.longRangeValidator2 = lrv;
    }
    private TextField TFNombreusuario = new TextField();

    public TextField getTFNombreusuario() {
        return TFNombreusuario;
    }

    public void setTFNombreusuario(TextField tf) {
        this.TFNombreusuario = tf;
    }

    // </editor-fold>
    /**
     * <p>Construct a new Page bean instance.</p>
     */
    public A_Usuario() {
    }

    /**
     * <p>Callback method that is called whenever a page is navigated to,
     * either directly via a URL, or indirectly via page navigation.
     * Customize this method to acquire resources that will be needed
     * for event handlers and lifecycle methods, whether or not this
     * page is performing post back processing.</p>
     * 
     * <p>Note that, if the current request is a postback, the property
     * values of the components do <strong>not</strong> represent any
     * values submitted with this request.  Instead, they represent the
     * property values that were saved for this view when it was rendered.</p>
     */
    @Override
    public void init() {
        // Perform initializations inherited from our superclass
        super.init();
        // Perform application initialization that must complete
        // *before* managed components are initialized
        // TODO - add your own initialiation code here

        // <editor-fold defaultstate="collapsed" desc="Managed Component Initialization">
        // Initialize automatically managed components
        // *Note* - this logic should NOT be modified
        try {
            _init();
        } catch (Exception e) {
            log("A_Usuario Initialization Failure", e);
            throw e instanceof FacesException ? (FacesException) e : new FacesException(e);
        }

        // </editor-fold>
        // Perform application initialization that must complete
        // *after* managed components are initialized
        // TODO - add your own initialization code here
    }

    /**
     * <p>Callback method that is called after the component tree has been
     * restored, but before any event processing takes place.  This method
     * will <strong>only</strong> be called on a postback request that
     * is processing a form submit.  Customize this method to allocate
     * resources that will be required in your event handlers.</p>
     */
    @Override
    public void preprocess() {
    }

    /**
     * <p>Callback method that is called just before rendering takes place.
     * This method will <strong>only</strong> be called for the page that
     * will actually be rendered (and not, for example, on a page that
     * handled a postback and then navigated to a different page).  Customize
     * this method to allocate resources that will be required for rendering
     * this page.</p>
     */
    @Override
    public void prerender() {
    }

    /**
     * <p>Callback method that is called after rendering is completed for
     * this request, if <code>init()</code> was called (regardless of whether
     * or not this was the page that was actually rendered).  Customize this
     * method to release resources acquired in the <code>init()</code>,
     * <code>preprocess()</code>, or <code>prerender()</code> methods (or
     * acquired during execution of an event handler).</p>
     */
    @Override
    public void destroy() {

        usuarioComboUSERNAMEDataProvider.close();

    }

    /**
     * <p>Return a reference to the scoped data bean.</p>
     *
     * @return reference to the scoped data bean
     */
    protected ApplicationBean1 getApplicationBean1() {
        return (ApplicationBean1) getBean("ApplicationBean1");
    }

    /**
     * <p>Return a reference to the scoped data bean.</p>
     *
     * @return reference to the scoped data bean
     */
    protected SessionBean1 getSessionBean1() {
        return (SessionBean1) getBean("SessionBean1");
    }

    /**
     * <p>Return a reference to the scoped data bean.</p>
     *
     * @return reference to the scoped data bean
     */
    protected RequestBean1 getRequestBean1() {
        return (RequestBean1) getBean("RequestBean1");
    }

    public String HPL_ASocio_action() {
        // TODO: Process the action. Return value is a navigation
        // case name where null will return to the same page.
        return "case1";
    }

    public String hyperlink2_action() {
        // TODO: Process the action. Return value is a navigation
        // case name where null will return to the same page.
        return "case2";
    }

    public String HPL_ADeposito_action() {
        // TODO: Process the action. Return value is a navigation
        // case name where null will return to the same page.
        return "case3";
    }

    public String HPL_Vender_action() {
        // TODO: Process the action. Return value is a navigation
        // case name where null will return to the same page.
        return "case5";
    }

    public void t_Pass1_processValueChange(ValueChangeEvent event) {
    }

    public String tabMUsuario_action() {
        alertaContraseñasMU.setVisible(false);
        alertaUsuarioModificado.setVisible(false);
        alertaContraseñaMU.setVisible(false);
        TFNombreusuario.setText("_");
        TFApellidousuario.setText("_");
        TFUsername.setText("A");
        TFPass1.setText("A");
        TFPass2.setText("A");
        

        return null;
    }
    public String HPL_AUsuario1_action() {
        // TODO: Replace with your code
        return null;
    }

    public String HPL_AExposicion1_action() {
        // TODO: Process the action. Return value is a navigation
        // case name where null will return to the same page.
        return "case4";
    }

    public String tabCUsuario_action() {
        alertaContraseñasGU.setVisible(false);
        alertausuarioGuardado.setVisible(false);
        return null;
    }

    public String b_Guardarusuario_action() {
        //*****BOTON GUARDAR USUARIO*****

        Nombreu = (String) TFNombreusuario.getText();
        Apellidou = (String) TFApellidousuario.getText();
        Username = (String) TFUsername.getText();
        Contraseña = (String) TFPass1.getText();
        Tipo = (String) CBTipoUsuario.getSelected();

        if (TFPass1.getText().equals(TFPass2.getText())) {
            alertaContraseñasGU.setVisible(false);
            alertausuarioGuardado.setVisible(true);
            SQL = "INSERT INTO Usuario VALUES ('" + Username + "','" + Nombreu + "','" + Apellidou + "','" + Tipo + "','" + Contraseña + "',TRUE);";
            in.insertar(SQL);
            TFNombreusuario.setText(null);
            TFApellidousuario.setText(null);
            TFUsername.setText(null);
            usuarioComboUSERNAMEDataProvider.refresh();

        } else {
            alertaContraseñasGU.setVisible(true);
            alertausuarioGuardado.setVisible(false);
        }


        return null;
    }

    public String tabMUsuario1_action() {
        // TODO: Process the action. Return value is a navigation
        // case name where null will return to the same page.
        return null;
    }

    public String b_Desactivarusuario1_action() {
        //*****BOTON DESACTIVAR USUARIO*****

        DContraseña = (String) t_Pass3.getText();
        if (DContraseña.equals("Fabio")) {
            Usernamemod = (String) CBSeleccion_usuario1.getValue();
            SQL = "UPDATE Usuario SET usuario_activo = FALSE WHERE username ='" + Usernamemod + "';";
            System.out.println(SQL);
            in.insertar(SQL);
            usuarioComboUSERNAMEDataProvider.refresh();
            alertaUsuarioDesactivado.setVisible(true);
            alertaContraseñaDes.setVisible(false);
        } else {
            alertaContraseñaDes.setVisible(true);
            alertaUsuarioDesactivado.setVisible(false);
        }

        return null;
    }

    public void TFApellidousuario_processValueChange(ValueChangeEvent vce) {
    }

    public String b_Modificarusuario1_action() {

        DContraseña = (String) TFPass3.getText();
        if (DContraseña.equals("Fabio")) {
            System.out.println("Entro");

            NNombreu = (String) TFNombreusuario_m.getText();
            NApellidou = (String) TFApellidousuario_m.getText();
            NContraseña = (String) TFNPASS.getText();
            NTipo = (String) CBTipoUsuario1.getValue();
            Usernamemod = (String) CBSeleccion_usuario.getValue();
            if (NNombreu != null) {
                SQL = "UPDATE usuario SET nombres_usuario = '" + NNombreu + "' WHERE username ='" + Usernamemod + "';";
                in.insertar(SQL);
                usuarioComboUSERNAMEDataProvider.refresh();
            }
            if (NApellidou != null) {
                SQL = "UPDATE usuario SET apellidos_usuario = '" + NApellidou + "' WHERE username ='" + Usernamemod + "';";
                in.insertar(SQL);
                usuarioComboUSERNAMEDataProvider.refresh();
            }

            if (NTipo != null) {
                SQL = "UPDATE usuario SET tipo_usuario = '" + NTipo + "' WHERE username ='" + Usernamemod + "';";
                in.insertar(SQL);
                usuarioComboUSERNAMEDataProvider.refresh();
            }
            System.out.println(NNombreu + NApellidou + NContraseña + NTipo + NContraseña);
            alertaContraseñasMU.setVisible(false);
            alertaUsuarioModificado.setVisible(true);
            alertaContraseñaMU.setVisible(false);

            if (NContraseña != null) {
                if (TFNPASS.getText().equals(TFNPASS2.getText())) {

                    SQL = "UPDATE usuario SET contraseña = '" + NContraseña + "' WHERE username ='" + Usernamemod + "';";
                    in.insertar(SQL);
                    usuarioComboUSERNAMEDataProvider.refresh();

                } else {
                    alertaContraseñasMU.setVisible(true);
                    alertaUsuarioModificado.setVisible(false);
                    alertaContraseñaMU.setVisible(false);
                }
            }
        } else {
            alertaContraseñasMU.setVisible(false);
            alertaUsuarioModificado.setVisible(false);
            alertaContraseñaMU.setVisible(true);
        }

        TFNombreusuario_m.setText(null);
        TFApellidousuario_m.setText(null);
        return null;
    }

    public String tabDesUsuario_action() {
        alertaContraseñaDes.setVisible(false);
        alertaUsuarioDesactivado.setVisible(false);
        return null;
    }

    public String alertaUsuarioModificado_action() {
        // TODO: Replace with your code
        return null;
    }

  


}
